package com.hdaccp.xingmu.controller;

import com.hdaccp.xingmu.controller.HouseCon;
import com.hdaccp.xingmu.dao.salesrecordDao;
import com.hdaccp.xingmu.dao.userdetailsDao;
import com.hdaccp.xingmu.entity.House;
import com.hdaccp.xingmu.entity.Property;
import com.hdaccp.xingmu.entity.Salesrecord;
import com.hdaccp.xingmu.entity.Userdetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/salesrecord")
public class SalesrecordCon {
    @Autowired
    private salesrecordDao salesrecordDao;

    @Autowired
    private userdetailsDao userdetailsDao;
    @Autowired
    private HouseCon house;
    @PersistenceContext
    private EntityManager entityManager;

//
    @RequestMapping("/quan")
    @ResponseBody
    public Salesrecord list(String number)
    {
        return salesrecordDao.queryByIdNative(number);
    }

    @RequestMapping("/add")
    @ResponseBody
    public Map<String,Object> map(Salesrecord salesrecord){
        Salesrecord s = new Salesrecord();
        s.setNumber(salesrecord.getNumber());
        s.setName(salesrecord.getName());
        s.setSfz(salesrecord.getSfz());
        s.setChanquan(salesrecord.getChanquan());
        s.setRemark(salesrecord.getRemark());
        s.setPhone(salesrecord.getPhone());
        s.setZdate(salesrecord.getZdate());
        s.setKdate(salesrecord.getKdate());
        s.setMoney(salesrecord.getMoney());
        s.setHouse(salesrecord.getHouse());

        System.out.println(salesrecord.getHouse().getId());


        if(salesrecord.getId()==-1){

        }else if (salesrecord.getId()!=-1){
            s.setId(salesrecord.getId());
        }
        Salesrecord salesrecord1 = salesrecordDao.save(s);


        //        添加住户
        Userdetails userdetails = new Userdetails();

        House h = new House();
        h.setId(salesrecord.getHouse().getId());

        userdetails.setHouse(h);

        userdetails.setName(s.getName());
        userdetails.setPhone(s.getPhone());

        Property p = new Property();
        p.setId(salesrecord.getChanquan());
        userdetails .setProperty(p);

        userdetails.setSfz(s.getSfz());

        userdetails.setRemarks(s.getRemark());

        String sql = "from Userdetails where houseid = "+h.getId();

        Query query = entityManager.createQuery(sql);

        List<Userdetails> uu = query.getResultList();

        if(uu.size()>0){
            Userdetails u2 = uu.get(0);
            if(uu.get(0)!=null){

                u2.setName(userdetails.getName());
                u2.setPhone(userdetails.getPhone());
                u2.setRundate(userdetails.getRundate());
                u2.setSfz(userdetails.getSfz());
                u2.setProperty(userdetails.getProperty());
                u2.setHouse(userdetails.getHouse());
                u2.setRemarks(userdetails.getRemarks());
                System.out.println("-----------------------------------------------------------------------------------------------------------------");
                System.out.println("1");
                System.out.println("-----------------------------------------------------------------------------------------------------------------");
                userdetailsDao.save(u2);
            }else{
                u2 = userdetails;
                System.out.println("-----------------------------------------------------------------------------------------------------------------");
                System.out.println("2");
                System.out.println("-----------------------------------------------------------------------------------------------------------------");
                userdetailsDao.save(userdetails);
            }

        }else{
            userdetailsDao.save(userdetails);
        }
        System.out.println("jiahao.123");
        return house.list("-1",null,"-1","-1",1,15);

    }

}
